<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- based on Roslyn's build/Targets/GenerateInternalsVisibleTo.targets -->

  <PropertyGroup>
    <MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
  </PropertyGroup>

  <PropertyGroup>
    <GeneratedInternalsVisibleToFile>$(IntermediateOutputPath)GeneratedInternalsVisibleTo$(DefaultLanguageSourceExtension)</GeneratedInternalsVisibleToFile>
  </PropertyGroup>

  <PropertyGroup>
    <CoreCompileDependsOn>GenerateInternalsVisibleToFile;$(CoreCompileDependsOn)</CoreCompileDependsOn>
  </PropertyGroup>

  <PropertyGroup Condition="'$(SignAssembly)' == 'True'">
    <MonoDevelopPublicKey>002400000c800000940000000602000000240000525341310004000001000100e1290d741888d13312c0cd1f72bb843236573c80158a286f11bb98de5ee8acc3142c9c97b472684e521ae45125d7414558f2e70ac56504f3e8fe80830da2cdb1cda8504e8d196150d05a214609234694ec0ebf4b37fc7537e09d877c3e65000f7467fa3adb6e62c82b10ada1af4a83651556c7d949959817fed97480839dd39b</MonoDevelopPublicKey>
    <InternalsVisibleToSuffix>, PublicKey=</InternalsVisibleToSuffix>
  </PropertyGroup>

  <ItemDefinitionGroup>
    <InternalsVisibleTo>
      <Visible>false</Visible>
      <PublicKey Condition="%(PublicKey)==''">$(MonoDevelopPublicKey)</PublicKey>
    </InternalsVisibleTo>
  </ItemDefinitionGroup>

  <!-- Dependency on PrepareForBuild is necessary so that we don't accidentally get ordered before it.
  We rely on PrepareForBuild to create the IntermediateOutputDirectory if it doesn't exist. -->
  <Target Name="GenerateInternalsVisibleToFile"
          Inputs="$(MSBuildThisFileFullPath);$(MSBuildProjectFile)"
          Outputs="$(GeneratedInternalsVisibleToFile)"
          DependsOnTargets="PrepareForBuild"
          Condition="'@(InternalsVisibleTo)' != ''">

    <!--
         This is a slightly evil trick. What we have is a group of InternalsVisibleTo items which
         we need to convert to the items that the WriteCodeFragment expects. They look like this:

         <InternalsVisibleTo Include="Foo" />

         And need to convert it to:

         <InternalsVisibleToAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
           <_Parameter1>Foo</Parameter1>
         </InternalsVisibleToAttribute>

         One way (although a bit evil) is to use the old CreateItem task. Since we use the well-defined
         "Identity" metadata on each of our itemgroups, MSBuild batching will take effect and this task
         will be invoked once for each InternalsVisibleTo item.
    -->
    <CreateItem Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute"
                AdditionalMetadata="_Parameter1=%(InternalsVisibleTo.Identity)$(InternalsVisibleToSuffix)%(InternalsVisibleTo.PublicKey)"
                Condition="'@(InternalsVisibleTo)' != ''">
      <Output TaskParameter="Include" ItemName="InternalsVisibleToAttribute" />
    </CreateItem>

    <WriteCodeFragment
        AssemblyAttributes="@(InternalsVisibleToAttribute)"
        Language="$(Language)"
        OutputFile="$(GeneratedInternalsVisibleToFile)">
      <Output TaskParameter="OutputFile" ItemName="Compile" />
      <Output TaskParameter="OutputFile" ItemName="FileWrites" />
    </WriteCodeFragment>

  </Target>
</Project>
